from rest_framework import serializers
from rest_framework.serializers import ModelSerializer

from main.models import OverworkSignup


class OverworkSignupSerializer(ModelSerializer):
    work_start_at = serializers.DateTimeField('%Y-%m-%d %H:%M:%S')
    work_end_at = serializers.DateTimeField('%Y-%m-%d %H:%M:%S')
    rest_start_at = serializers.DateTimeField('%Y-%m-%d %H:%M:%S')
    rest_end_at = serializers.DateTimeField('%Y-%m-%d %H:%M:%S')
    user = serializers.CharField(default=serializers.CurrentUserDefault())
    user_id = serializers.CharField(read_only=True, source='user.id')

    class Meta:
        model = OverworkSignup
        fields = '__all__'


class OverworkSignupExportSerializer(ModelSerializer):
    开始日期 = serializers.DateTimeField('%Y-%m-%d', source='work_start_at')
    开始时间 = serializers.DateTimeField('%H:%M:%S', source='work_start_at')
    结束日期 = serializers.DateTimeField('%Y-%m-%d', source='work_end_at')
    结束时间 = serializers.DateTimeField('%H:%M:%S', source='work_end_at')
    休息开始时间 = serializers.DateTimeField('%H:%M:%S', source='work_start_at')
    休息结束时间 = serializers.DateTimeField('%H:%M:%S', source='work_end_at')
    姓名 = serializers.CharField(source='user.username')
    工号 = serializers.CharField(source='user.employee_id')
    加班类型 = serializers.CharField(source='content')

    class Meta:
        model = OverworkSignup
        fields = ('工号', '姓名', '开始日期', '开始时间', '结束日期', '结束时间', '休息开始时间', '休息结束时间', '加班类型',)


class OverworkSignupWithRawDatetimeSerializer(ModelSerializer):

    class Meta:
        model = OverworkSignup
        fields = '__all__'

    def to_representation(self, instance):
        res = super().to_representation(instance)
        res['work_start_at'] = res['work_start_at'][:-4]
        res['work_end_at'] = res['work_end_at'][:-4]
        res['rest_start_at'] = res['rest_start_at'][:-4]
        res['rest_end_at'] = res['rest_end_at'][:-4]
        return res
